//点击表格数据跳转
//getDetail = function () { 如果是这种写法的话，则出现的bug是需要点两次才生效
ondelete = function () {
    let tbody = document.getElementsByClassName("_tbody");
    let tr = tbody[0].getElementsByTagName("tr");
    for (let i = 0; i < tr.length; i++) {
        let id = tr[i].getElementsByTagName("td")[1].innerText;
        tr[i].getElementsByTagName("td")[3].getElementsByTagName("a")[1].onclick = function () {
            $.ajax({
                url: "/delete/",
                type: "get",
                data: {"id": id},
                success: function () {
                    location.reload();
                    alert("删除成功！");
                }
            })
        };
    }
};
get_detail = function () {
    let tbody = document.getElementsByClassName("_tbody");
    let tr = tbody[0].getElementsByTagName("tr");
    for (let i = 0; i < tr.length; i++) {
        tr[i].getElementsByTagName("td")[2].onclick = function () {
            let id = tr[i].getElementsByTagName("td")[1].innerText;
            window.location.href = "/detail?id=" + id;
        };
    }
};

selOne = function () {
    let check_item = 0;
    let unSelCount = 0;
    let selects = document.getElementsByName("record");
    for (let i = 0; i < selects.length; i++) {
        if (selects[i].checked) {
            check_item++;
        }
        if (!selects[i].checked) {
            unSelCount++;
        }
    }
    if (check_item === selects.length) {
        $("#batch_archive").prop("disabled", false);
        $("#exprot").prop("disabled", false);
        $("#checkAll").prop("checked", true);
    }
    if (selects.length > check_item > 0) {
        $("#checkAll").prop("checked", false);
        $("#exprot").prop("disabled", false);
        $("#batch_archive").prop("disabled", false);
    }
    if (unSelCount === selects.length) {
        $("#checkAll").prop("checked", false);
        $("#batch_archive").prop("disabled", true);
        $("#exprot").prop("disabled", true);

    }
};
checkall = function () {
    $("#checkAll").click(function () {
        if (this.checked) {
            $("input[name='record']").prop("checked", true);
            $("#batch_archive").prop("disabled", false);
            $("#exprot").prop("disabled", false);
        } else {
            $("input[name='record']").prop("checked", false);
            $("#batch_archive").prop("disabled", true);
            $("#exprot").prop("disabled", true);
        }
    })
};
let data_id;
onarchive = function () {
    let sels = $("input[name='record']:checked");
    let str = "";
    let count = 0;
    for (let i = 0; i < sels.length; i++) {
        if (sels[i].checked) {
            str += sels[i].value + ",";
            count++;
        }
    }
    if (count === 0) {
        alert("没有选中任何数据，请先选中数据！");
    }
    //判断有选中的数据在向服务端发起请求
    if (count > 0) {
        $.ajax({
            url: "/archive/",
            type: 'get',
            data: {"str": str},
            async: false,
            success: function (data) {
                // location.href = "/index";
                data = JSON.parse(data);
                let gameprojects = data["gameprojects"];
                data_id = data["data_id"];
                let str = "";
                for (let i = 0; i < gameprojects.length; i++) {
                    let project_name = gameprojects[i]['project_name'];
                    let id = gameprojects[i]['id'];
                    let create = gameprojects[i]['create_time'];
                    str += "<tr>" + "<td>" +
                        "<label>" +
                        "<input type='checkbox' name='sleOne_archive' id='sleOne_archive' value='" + id + "' >" +
                        "</label>" +
                        "</td>" +
                        "<td>" + project_name + "</td>" +
                        "<td>" + id + "</td>" +
                        "<td>" + create + "</td>" +
                        "</tr>";
                }
                document.getElementById("gameproject").innerHTML = str;
            }
        })
    }
};
getMydata = function () {
    location.href = '/index/';
};
addtask = function () {
    let task = document.getElementById("task-name").value;
    if (!task) {
        alert("输入任务名不能为空");
    } else {
        $.ajax({
            url: "/addtask/",
            type: "post",
            data: {"task": task},
            success: function (data) {
                $("#exam-modal").modal("hide");
                //    todo 刷新导航列表内容
                location.reload();
            }
        })
    }
};

addarchive = function () {
    let ids = $.extend(true, [], data_id);//需要使用深拷贝
    if (ids.length !== 0) {
        data_id.length = 0;
    }
    let arr = [];
    let id_list = [];
    let selects = document.getElementsByName("sleOne_archive");
    let uncount = 0;
    for (let i = 0; i < selects.length; i++) {
        if (!selects[i].checked) {
            uncount++;
        } else {
            arr.push(selects[i].value);
        }
    }
    console.log(arr);
    if (uncount === selects.length) {
        alert("未选择任务数据！")
    } else {
        $.ajax({
            url: "/submitarchive/",
            type: "post",
            traditional: true,
            data: {
                "ids": ids,
                "arr": arr,
            },
            dataType: "json",
            async: true,
            success: function (data) {
                if (data === 'ok') {
                    alert("归档成功！");
                    location.reload();
                } else {
                    alert("数据id:" + data["data"] + "已经归档过了！");
                    location.reload();
                }
            }
        });
    }
};

//点击导航栏中的项目
onproject = function () {
    let task_name = "";
    let ul = document.getElementById("navhead");
    let li = ul.getElementsByTagName("li");
    for (let i = 0; i < li.length; i++) {
        li[i].onclick = function () {
            task_name = li[i].getElementsByTagName("a")[0].innerText;
            if (task_name !== "") {
                $.ajax({
                    url: "/index/" + task_name,
                    type: "get",
                    success: function (data) {
                        location.href = '/index/' + task_name;
                    }
                })
            }
        }
    }
};

//==========================
together = function () {
    let elements = document.getElementsByName("project_record");
    let arr = [];
    let project_name = document.getElementById("project_text").innerText;
    let count = 0;
    for (let i = 0; i < elements.length; i++) {
        console.log(elements[i].checked);
        if (elements[i].checked) {
            arr.push(elements[i].getAttribute("value"));
            count++;
        } else {
            count += 0;
        }
    }
    if (count === 0) {
        alert("还没有选择任何数据！")
    } else {
        if (arr !== null) {
            console.log(arr);
            console.log(project_name);
            $.ajax({
                url: "/get_together/",
                type: "get",
                traditional: true,
                data: {
                    "arr": arr,
                    "project_name": project_name,
                },
                success: function (data) {
                    if (data !== "none") {
                        let storage = window.localStorage;
                        storage.setItem("data", data);
                        storage.setItem('project_id', JSON.stringify(arr));
                        storage.setItem('project_name', project_name);
                        location.href = "/togetherdetail/";
                    } else {
                        alert("聚合的数据为空！")
                    }
                }
            })
        }
    }
};
